package com.yanqu.road.server.cmd.activity.doublespring;

import com.yanqu.road.entity.activity.doublespring.data.DoubleSpringUserInviteData;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DoubleSpringProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.doublespring.CrossDoubleSpringActivity;
import com.yanqu.road.server.manager.activity.doublespring.CrossDoubleSpringActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.Map;

@Cmd(code = Protocol.C_CROSS_DOUBLE_SPRING_CANCEL_INVITE, desc = "踏春游记-取消邀请")
public class CrossDoubleSpringCancelInviteCmd extends CrossCommand {

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DoubleSpringProto.DoubleSpringCancelInviteUserReqMsg reqMsg = DoubleSpringProto.DoubleSpringCancelInviteUserReqMsg.parseFrom(packet.getBytes());
        CrossDoubleSpringActivity activity = CrossDoubleSpringActivityMgr.getActivity(reqMsg.getActivityId());
        if (activity == null) {
            ret = GameErrorCode.E_DOUBLE_SPRING_ACTIVITY_NOT_EXIST;
            return;
        }
        DoubleSpringProto.DoubleSpringCancelInviteCrossToServerMsg.Builder respMsg = DoubleSpringProto.DoubleSpringCancelInviteCrossToServerMsg.newBuilder();
        respMsg.setActivityId(reqMsg.getActivityId());
        //取消邀请
        if (reqMsg.getAllCancelFlag()) {
            //全部取消
            Map<Long, DoubleSpringUserInviteData> inviteMap = activity.cancelInviteAll(packet.getPlayerId());
            if (inviteMap != null) {
                for (long inviteUserId : inviteMap.keySet()) {
                    respMsg.addCancelUserId(inviteUserId);
                }
            }
        } else {
            //取消单个
            DoubleSpringUserInviteData data = activity.cancelInvite(packet.getPlayerId(), reqMsg.getUserId());
            if (data != null) {
                respMsg.addCancelUserId(data.getInviteUserId());
            }
        }
        //pb返回
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_CANCEL_INVITE_FROM_CROSS, respMsg));
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            DoubleSpringProto.DoubleSpringCancelInviteUserRespMsg.Builder respMsg = DoubleSpringProto.DoubleSpringCancelInviteUserRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_DOUBLE_SPRING_CANCEL_INVITE, respMsg));
        }
    }
}
